package view.project;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dal.project.Project;
import dal.project.ProjectUtil;

/**
 * Servlet implementation class GetProjectNameByID
 */
@WebServlet("/GetProjectNameByID")
public class GetProjectNameByID extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetProjectNameByID() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter printWriter=response.getWriter();
		
		
		JsonObject myJsonObject=new JsonObject();
		String projectName="";
		
		try {
			
			response.setHeader("Cache-control", "no-cache, no-store");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Expires", "-1");
			
			response.setHeader("Access-Control-Allow-Origins", "*");
			response.setHeader("Access-Control-Allow-Methods", "POST");
			response.setHeader("Access-Control-Allow-Headers", "Content-Type");
			response.setHeader("Access-Control-Allow-Max-Age", "86400");
			
			
			String projectId=request.getParameter("project_id");
			
			//Gson gson=new Gson();
			
			try {
				Project project=(Project)new ProjectUtil().get(projectId);
				
				if(project!=null){
					projectName=project.getTitle();
				}else if(project==null){
					projectName="N/A";
				}
			} catch (Exception e) {
				projectName="N/A";
			}
			
			myJsonObject.addProperty("projectName",projectName);
			printWriter.print(myJsonObject.toString());
			printWriter.close();
			
			
		} catch (Exception e) {
			projectName="N/A";
		}
	}

}
